package com.sofwin.controller;

import com.sofwin.AjaxResult;
import com.sofwin.system.pojo.SysDept;
import com.sofwin.system.service.DeptService;
import com.sofwin.system.vo.DeptTreeVO;
import com.sofwin.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @packageName: com.sofwin.controller
 * @user: andyliu
 * @date: 2021/7/16 9:53
 * @email 115176513@qq.com
 * @description: TODO
 */
@RestController
@RequestMapping("dept")
public class DeptController {
    @Autowired
    private DeptService service;
    @RequestMapping("data")
    public List<SysDept> data(SysDept dept){
        return service.queryDepts(dept);
    }

    @PostMapping("treeData")
    public AjaxResult treeData(){
        List<DeptTreeVO> depts = service.queryTreeData();
        return AjaxResult.success(depts);
    }


    @GetMapping("checkUnique")
    public boolean checkUnique(SysDept dept){
        boolean status = true;
        List<SysDept> depts = service.queryDepts(dept);
        if(depts!=null && depts.size()==1){
            status = false;
        }
        return  status;

    }
    @PostMapping("save")
    public AjaxResult saveOrUpdate(SysDept dept){
        dept.setCreateDate(DateUtil.getCurrentDateStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
        boolean status = service.saveOrUpdate(dept);
        if(status){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
